#!/usr/bin/env python3
# coding:utf-8

import os
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QCheckBox,
    QVBoxLayout,
    QHBoxLayout,
    QPushButton,
    QLineEdit,
    QFileDialog,
)
from PyQt5.QtGui import QFont


class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.rsync_opt = {
            "-a": False,
            "-r": False,
            "-P": False,
        }
        self.initUI()

    def initUI(self):
        # 设置字体
        font = QFont()
        font.setFamily(
            "wqy-microhei"
        )  # 设置字体为宋体，可替换成你需要的中文字体，如'黑体'、'微软雅黑'
        font.setPointSize(10)  # 设置字体大小
        self.font = font

        # 创建文本输入框
        self.LineEdit_src = QLineEdit()
        self.LineEdit_src.setPlaceholderText("请输入源路径")
        self.LineEdit_dst = QLineEdit()
        self.LineEdit_dst.setPlaceholderText("请输入目的路径")
        self.file_btn_src = QPushButton("file")
        self.file_btn_src.clicked.connect(self.callback_file_button_src)
        self.folder_btn_src = QPushButton("dir")
        self.folder_btn_src.clicked.connect(self.callback_folder_button_src)
        h_layout_src = QHBoxLayout()
        h_layout_src.addWidget(self.LineEdit_src)
        h_layout_src.addWidget(self.file_btn_src)
        h_layout_src.addWidget(self.folder_btn_src)

        self.file_btn_dst = QPushButton("file")
        self.file_btn_dst.clicked.connect(self.callback_file_button_dst)
        self.folder_btn_dst = QPushButton("dir")
        self.folder_btn_dst.clicked.connect(self.callback_folder_button_dst)
        h_layout_dst = QHBoxLayout()
        h_layout_dst.addWidget(self.LineEdit_dst)
        h_layout_dst.addWidget(self.file_btn_dst)
        h_layout_dst.addWidget(self.folder_btn_dst)

        # 开关按钮 -a
        self.checkBox_opt_a = QCheckBox("-a :递归,时间戳,权限chmod,用户/组chown,链接")
        self.checkBox_opt_a.stateChanged.connect(self.handler_rsync_opt_a)
        self.checkBox_opt_a.setFont(font)

        # 开关按钮 -r
        self.checkBox_opt_r = QCheckBox("-r :递归,权限chmod")
        self.checkBox_opt_r.stateChanged.connect(self.handler_rsync_opt_r)
        self.checkBox_opt_r.setFont(font)

        # 开关按钮 -P
        self.checkBox_opt_P = QCheckBox("-P :显示进度")
        self.checkBox_opt_P.stateChanged.connect(self.handler_rsync_opt_P)
        self.checkBox_opt_P.setFont(font)

        # 创建按钮
        self.button_start = QPushButton("start/stop")
        self.button_start.clicked.connect(self.handler_start_stop)

        layout = QVBoxLayout()
        layout.addLayout(h_layout_src)
        layout.addLayout(h_layout_dst)
        layout.addWidget(self.LineEdit_src)
        layout.addWidget(self.LineEdit_dst)
        layout.addWidget(self.checkBox_opt_a)
        layout.addWidget(self.checkBox_opt_r)
        layout.addWidget(self.checkBox_opt_P)
        layout.addWidget(self.button_start)
        self.setLayout(layout)

        self.setWindowTitle("rsync UI")
        self.setGeometry(300, 300, 200, 150)
        self.show()

    def callback_file_button_src(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", os.getcwd())
        if file_path:
            self.LineEdit_src.setText(file_path)

    def callback_folder_button_src(self):
        folder_path = QFileDialog.getExistingDirectory(self, "选择文件", os.getcwd())
        if folder_path:
            self.LineEdit_src.setText(folder_path)

    def callback_file_button_dst(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择文件", os.getcwd())
        if file_path:
            self.LineEdit_dst.setText(file_path)

    def callback_folder_button_dst(self):
        folder_path = QFileDialog.getExistingDirectory(self, "选择文件", os.getcwd())
        if folder_path:
            self.LineEdit_dst.setText(folder_path)

    def handler_rsync_opt_a(self, state):
        if state == 2:
            self.rsync_opt["-a"] = True
        else:
            self.rsync_opt["-a"] = False
        print("opt -a:", self.rsync_opt["-a"])

    def handler_rsync_opt_r(self, state):
        if state == 2:
            self.rsync_opt["-r"] = True
        else:
            self.rsync_opt["-r"] = False
        print("opt -r:", self.rsync_opt["-r"])

    def handler_rsync_opt_P(self, state):
        if state == 2:
            self.rsync_opt["-P"] = True
        else:
            self.rsync_opt["-P"] = False
        print("opt -P:", self.rsync_opt["-P"])

    def handler_start_stop(self, state):
        opt = ""
        if self.rsync_opt["-P"]:
            opt += "-P "
        if self.rsync_opt["-r"]:
            opt += "-r "
        if self.rsync_opt["-a"]:
            opt += "-a "

        src = self.LineEdit_src.text()
        dst = self.LineEdit_dst.text()
        cmd = f"rsync {opt} {src} {dst}"
        print("cmd:", cmd)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
